﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ODP.Interface;
using ODP.Model;
using System.Diagnostics;
using ODP.Enum;
using ODP.Com;

namespace ODP.SinaAPI
{
    public class SinaAPI : IAPI
    {
        #region 获取请求资源的url字符串
        /// <summary>
        /// 获取请求资源的url字符串
        /// </summary>
        /// <param name="tt"></param>
        /// <param name="oauth"></param>
        /// <param name="requestBaseUrl"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public APIInfo GetUrl(ref string requestUrlNoParas, ref string queryString, Enum.TwitterType tt, OAuthBase oauth, int framelevel, params Model.QueryStringParameter[] paras)
        {
            StringBuilder result = new StringBuilder();

            List<QueryStringParameter> qspList = new List<QueryStringParameter>();

            if (paras != null && paras.Length > 0)
            {
                qspList = paras.ToList();
            }

            System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(System.Threading.Thread.CurrentThread, false);
            StackFrame sf = st.GetFrame(framelevel);
            string functionName = sf.GetMethod().Name;

            //获取当前方法对应的url路径以及httpmethod
            APIInfo apiInfo = oauth.apiInfoList.Where(p => p.APIFunctionName == functionName).FirstOrDefault(); ;

            if (apiInfo != null)
            {
                requestUrlNoParas = oauth.baseurl + apiInfo.APIURLPath;

                oauth.GetRequestUrl(ref queryString, requestUrlNoParas, apiInfo.APIRequestMethod.ToString(), qspList);
 
            }

            return apiInfo;
        }

        public APIInfo GetUrl(ref string requestUrlNoParas, ref string queryString, Enum.TwitterType tt, OAuthBase oauth, params Model.QueryStringParameter[] paras)
        {
            return GetUrl(ref requestUrlNoParas, ref queryString, tt, oauth, 2, paras);
        }
        #endregion

        #region BaseRequest
        /// <summary>
        /// BaseRequest
        /// </summary>
        /// <param name="oauth"></param>
        /// <param name="tt"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public string BaseRequest(OAuthBase oauth, Enum.TwitterType tt, params Model.QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, 3, paras);
        }

        public string BaseRequest(OAuthBase oauth, Enum.TwitterType tt, int framelevel, params Model.QueryStringParameter[] paras)
        {
            string result = string.Empty;
            string requestUrlNoParas = string.Empty;
            string requestString = string.Empty;

            APIInfo apiInfo = GetUrl(ref requestUrlNoParas, ref requestString, tt, oauth, framelevel, paras);


            if (apiInfo.APIRequestMethod == HttpMethod.POST)
            {
                result = new SyncHttp().HttpPost(requestUrlNoParas, requestString);
            }
            else
            {
                result = new SyncHttp().HttpGet(requestUrlNoParas, requestString);
            }
            


            return result;
        }
        #endregion

        #region IAPI 成员

        public string GetHomePage(Com.OAuthBase oauth, Enum.TwitterType tt, params Model.QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        public string Forward(Com.OAuthBase oauth, Enum.TwitterType tt, params Model.QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        public string Mention(Com.OAuthBase oauth, Enum.TwitterType tt, params Model.QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        public string GetOtherUserInfo(Com.OAuthBase oauth, Enum.TwitterType tt, params Model.QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        public string WeiBoAdd(Com.OAuthBase oauth, Enum.TwitterType tt, params Model.QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        public string WeiBoAddWithPic(Com.OAuthBase oauth, Enum.TwitterType tt, List<Model.QueryStringParameter> files, params Model.QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        public string GetCurrentUserInfo(Com.OAuthBase oauth, Enum.TwitterType tt, params Model.QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        public string AddFriends(Com.OAuthBase oauth, Enum.TwitterType tt, params Model.QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        public string AddSpecialFriends(Com.OAuthBase oauth, Enum.TwitterType tt, params Model.QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        public string InviteFriends(Com.OAuthBase oauth, Enum.TwitterType tt, params Model.QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        public string GetOtherUserFans(Com.OAuthBase oauth, Enum.TwitterType tt, params Model.QueryStringParameter[] paras)
        {
            throw new NotImplementedException();
        }

        public string GetOtherUserSpecialFans(Com.OAuthBase oauth, Enum.TwitterType tt, params Model.QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        public string GetOtherUserIdol(Com.OAuthBase oauth, Enum.TwitterType tt, params Model.QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        public string GetCurrentUserFans(Com.OAuthBase oauth, Enum.TwitterType tt, params Model.QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        public string GetCurrentUserIdol(Com.OAuthBase oauth, Enum.TwitterType tt, params Model.QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        public string GetMayKownPerson(Com.OAuthBase oauth, Enum.TwitterType tt, params Model.QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        public string GetFamousList(Com.OAuthBase oauth, Enum.TwitterType tt, params Model.QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        public string GetHotPerson(Com.OAuthBase oauth, Enum.TwitterType tt, params Model.QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        public string GetMayKownPersonByContent(Com.OAuthBase oauth, Enum.TwitterType tt, params Model.QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }
        
        #endregion

        #region IAPI 成员


        public string Reply(OAuthBase oauth, TwitterType tt, params QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        public string RePost(OAuthBase oauth, TwitterType tt, params QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        public string AddComment(OAuthBase oauth, TwitterType tt, params QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        #endregion
    }
}
